//
//  LoanTypeView.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/11/9.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

@objc protocol LoanTypeViewDelegate {
    @objc optional func selectWorktype(index:Int,title:String) //选择职业类型
}

class LoanTypeView: UIView {
    
    weak var delegate : LoanTypeViewDelegate?

    fileprivate let cellId = "loanTypeCell"
    fileprivate let cellId2 = "loanTypeImageCell"
    fileprivate let dataArray = [creditLoans,mortgageLoan,students,freelance,working,businessOwners,individual]
    fileprivate let textArray = [hasCreditPoints,hasFixedAssets,hasCreditPoints,lendersOnTheDay,hasJobCertificate,hasBusinessLicense,hasBusinessLicense]
    fileprivate let images = ["s1","s2","s3","s4"]
    
    init() {
        super.init(frame: .zero)
        
        self.backgroundColor = UIColor.white
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = UIColor.clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.register(LoanTypeCell.self, forCellWithReuseIdentifier: cellId)
        collectionView.register(LoanTypeImageCell.self, forCellWithReuseIdentifier: cellId2)
        self.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview().inset(UIEdgeInsets.zero)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension LoanTypeView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        if indexPath.row < 4 {
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId2, for: indexPath) as? LoanTypeImageCell
            cell?.titleLabel.text = dataArray[indexPath.row]
            cell?.textLabel.text = textArray[indexPath.row]
            cell?.imageView.image = UIImage.init(named: images[indexPath.row])
            return cell!
        }else{
            let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as? LoanTypeCell
            cell?.titleLabel.text = dataArray[indexPath.row]
            cell?.textLabel.text = textArray[indexPath.row]
            return cell!
        }
        
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        collectionView.deselectItem(at: indexPath, animated: true)
        
        self.delegate?.selectWorktype!(index: indexPath.row, title: dataArray[indexPath.row])
        
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        
        if indexPath.row < 4 {
            return CGSize.init(width: ScreenW/2, height: RATIO_H(maxNum: 100))
        }else{
            return CGSize.init(width: ScreenW/3-1, height: RATIO_H(maxNum: 100))
        }
    }
    
}
